package com.jugheadzhou.aop.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

/**
 * 线程池配置
 *
 * @author jugheadzhou
 * @date 2021年04月10日
 */
@Configuration
@Slf4j
public class ThreadPoolConfig implements SchedulingConfigurer, AsyncConfigurer {

    /**
     * 默认-定时任务使用的线程池
     *
     * @return
     */
    @Bean(destroyMethod = "shutdown", name = "taskScheduler")
    public ThreadPoolTaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(11);
        scheduler.setThreadNamePrefix("task_pool-");
        scheduler.setAwaitTerminationSeconds(600);
        scheduler.setWaitForTasksToCompleteOnShutdown(true);
        return scheduler;
    }

    /**
     * 默认-异步任务执行线程池
     *
     * @return
     */
    //@Bean(name = "asyncExecutor")
//    public ThreadPoolTaskExecutor asyncExecutor() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setCorePoolSize(10);
//        executor.setQueueCapacity(1000);
//        executor.setKeepAliveSeconds(600);
//        executor.setMaxPoolSize(20);
//        executor.setThreadNamePrefix("asyncTask-");
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        executor.initialize();
//        return executor;
//    }

//    /**
//     * 危急值-定时任务使用的线程池
//     * 需要在使用异步任务指定线程池名称才能生效
//     *
//     * @return
//     */
//    @Bean("warnTaskExecutor")
//    public ThreadPoolTaskExecutor warnTaskExecutor() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        executor.setCorePoolSize(5);
//        executor.setQueueCapacity(500);
//        executor.setKeepAliveSeconds(600);
//        executor.setMaxPoolSize(10);
//        executor.setThreadNamePrefix("warnTask-");
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        executor.initialize();
//        return executor;
//    }

    /**
     * 设置默认定时任务线程池
     *
     * @return
     */
    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        ThreadPoolTaskScheduler taskScheduler = taskScheduler();
        scheduledTaskRegistrar.setTaskScheduler(taskScheduler);
    }

    /**
     * 设置默认异步任务线程池
     *
     * @return
     */
//    @Override
//    public Executor getAsyncExecutor() {
//        return asyncExecutor();
//    }

    /**
     * 异步任务日志打印
     *
     * @return
     */
//    @Override
//    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
//        return (throwable, method, objects) -> {
//            log.error("异步任务执行出现异常, message {}, method {}, params {}", throwable, method, objects);
//        };
//    }

}